package com.ls.controller;

import com.ls.annotation.LoginRequired;
import com.ls.entity.Comment;
import com.ls.entity.DiscussPost;
import com.ls.entity.Event;
import com.ls.entity.User;
import com.ls.event.EventProducer;
import com.ls.service.CommentService;
import com.ls.service.DiscussPostService;
import com.ls.util.CommunityConstant;
import com.ls.util.UserContextHolder;
import com.ls.util.RedisKeyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @author ls
 * @date 2020/12/26
 **/
@Controller
@RequestMapping("/comment")
public class CommentController implements CommunityConstant {

    @Resource
    CommentService commentService;

    @Resource
    private UserContextHolder userContextHolder;

    @Resource
    private EventProducer eventProducer;

    @Resource
    private DiscussPostService discussPostService;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    /**
     * 功能描述: <br>
     * 发布评论
	 * @param discussPostId
	 * @param comment
	 * @return {@link String}
     * @author ls
     * @date   2021/8/21 19:16
     */
    @LoginRequired
    @PostMapping("/add/{discussPostId}")
    public String addComment(@PathVariable("discussPostId") int discussPostId,
                             Comment comment) {
        User user = userContextHolder.getUser();
        comment.setUserId(user.getId());
        comment.setStatus(0);
        comment.setCreateTime(new Date());
        commentService.addComment(comment);

        // 触发评论事件
        Event event = new Event()
                .setTopic(TOPIC_COMMENT)
                .setUserId(user.getId())
                .setEntityType(comment.getEntityType())
                .setEntityId(comment.getEntityId())
                .setData("postId", discussPostId);
        if (comment.getEntityType() == ENTITY_TYPE_POST) {
            DiscussPost target = discussPostService.queryDiscussPostById(comment.getEntityId());
            event.setEntityUserId(target.getUserId());
        } else if (comment.getEntityType() == ENTITY_TYPE_COMMENT) {
            Comment target = commentService.queryCommentById(comment.getEntityId());
            event.setEntityUserId(target.getUserId());
        }
        eventProducer.fireEvent(event);

        // 如果是给帖子的评论 触发发帖事件
        if (comment.getEntityType() == ENTITY_TYPE_POST) {
            event = new Event()
                    .setTopic(TOPIC_PUBLISH)
                    .setUserId(user.getId())
                    .setEntityType(ENTITY_TYPE_POST)
                    .setEntityId(discussPostId);
            eventProducer.fireEvent(event);

            // 对帖子评论, 将帖子id存入redis, 计算帖子热度 分数
            String redisKey = RedisKeyUtil.getPostScoreKey();
            redisTemplate.opsForSet().add(redisKey, discussPostId);
        }

        return "redirect:/discuss/detail/" + discussPostId;
    }

}
